package com.wyw.learning.thread.lock.reentrantlock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 演示ReentrantLock基本用法，演示被打断的情况
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * @date 2021/7/21   11:43 下午
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class PrintStringLockDemo {

    public static void main(String[] args) {
        new PrintStringLockDemo().init();
    }

    private void init() {
        final Outputer outputer = new Outputer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outputer.output("悟空");
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outputer.output("大师兄");
                }

            }
        }).start();
    }

    static class Outputer{
        Lock lock = new ReentrantLock();

        //字符串打印方法，一个个字符打印
        public void output(String name){
            int len = name.length();

//            lock.lock();

            try{
                for (int i=0; i< len; i++){
                    System.out.print(name.charAt(i));
                }
                System.out.println("");
            } finally {
//                lock.unlock();
            }
        }
    }

}
